﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SlamCms.SharePoint
{
	public class SlamCmsModule : IHttpModule
	{
		private static object _lock = new object();
		private static bool _startWasCalled = false;
		//internal static ILifetimeScope AutofacRequestLifetimeScope
		//{
		//	get;
		//	private set;
		//}

		public void Dispose()
		{
		}

		public void Init(HttpApplication context)
		{
			//context.BeginRequest += OnBeginRequest;
			//context.EndRequest += OnEndRequest;
			context.Error += OnError;

			// Init is called once per HttpApplication instance so to execute code
			// once per AppDomain we use this lock
			//lock (_lock)
			//{
			//	if (!_startWasCalled)
			//	{
			//		_startWasCalled = true;
			//		this.OnStart(context);
			//	}
			//}
		}

		private void OnStart(HttpApplication context)
		{
			Bootstrapper.Run(context);
		}

		//private void OnBeginRequest(object sender, EventArgs e)
		//{	
		//}

		//static void OnEndRequest(object sender, EventArgs e)
		//{
		//	if (AutofacRequestLifetimeScope != null)
		//		AutofacRequestLifetimeScope.Dispose();
		//}

		private void OnError(object sender, EventArgs e)
		{
		}

		//public static void SetAutofacRequestLifetimeScope(ILifetimeScope requestlifetimeScope)
		//{
		//	AutofacRequestLifetimeScope = requestlifetimeScope;
		//}

		//private HttpContextBase GetHttpContext(object sender)
		//{
		//	var httpApplication = sender as HttpApplication;
		//	return new HttpContextWrapper(httpApplication.Context);
		//}
	}
}
